﻿@model DLMemberPointMS.Entity.ViewModel.PreInsertView

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>新增礼品</title>
    <link rel="stylesheet" type="text/css" href="~/layui/css/layui.css" />

    <script src="~/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="~/Scripts/jquery-3.3.1.js" type="text/javascript" charset="utf-8"></script>
    <style type="text/css">
        #BIaodan {
            margin-top:25px;
            margin-left:50px;
        }
    </style>
</head>
<body>

    @using (Html.BeginForm("", "", FormMethod.Post, new { @class = "", id = "BIaodan" }))
    {
        @Html.AntiForgeryToken()
        <div class="layui-form-item">
            <label class="layui-form-label">礼品代号</label>
            <div class="layui-input-inline">
                @Html.EditorFor(model => model.EG_GiftCode, new { htmlAttributes = new { @class = "layui-input" } })
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">礼品名称</label>
            <div class="layui-input-inline">
                @Html.EditorFor(model => model.EG_GiftName, new { htmlAttributes = new { @class = "layui-input" } })
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">礼品图片</label>
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="test1">上传图片</button>
                <div class="layui-upload-list">
                    <img src="@ViewBag.img" class="layui-upload-img" id="demo1" style="width:100px;height:100px;margin-left:110px;">
                   <input type="text" hidden id="imgPath" name="EG_Photo" />
                    <p id="demoText"></p>
                </div>
                </div>
                @*@Html.EditorFor(model => model.EG_Photo, new { htmlAttributes = new { @class = "layui-input" } })*@

            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所需积分</label>
                <div class="layui-input-inline">
                    @Html.EditorFor(model => model.EG_Point, new { htmlAttributes = new { @class = "layui-input" } })

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">礼品数量</label>
                <div class="layui-input-inline">
                    @Html.EditorFor(model => model.EG_Number, new { htmlAttributes = new { @class = "layui-input" } })

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline">
                    @Html.EditorFor(model => model.EG_Remark, new { htmlAttributes = new { @class = "layui-input" } })
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    
                    <button class="layui-btn" lay-submit lay-filter="submitform">提交</button>

                </div>
            </div>
            @Html.HiddenFor(Model => Model.EG_ID)
            }
            <script type="text/javascript">


                layui.use(['form', 'upload', 'layer'], function () {
                    var form = layui.form
                        , layer = layui.layer;
                    //图片上传
                    var $ = layui.jquery,
                        upload = layui.upload;
                     //普通图片上传
                    var uploadInst = upload.render({
                        elem: '#test1'
                        , url: '/Present/UploadImgRes'
                        , before: function (obj) {
                            //预读本地文件示例，不支持ie8
                            obj.preview(function (index, file, result) {
                                $('#demo1').attr('src', result); //图片链接（base64）
                            });
                        }
                        , done: function (res) {
                            //如果上传失败
                            if (res.code > 0) {
                                return layer.msg('上传失败',{icon:1});
                            }
                            //上传成功
                               var filePath = res.data;
                            $('#imgPath').val(filePath);
                            return layer.msg('上传成功', { icon: 1 });
                         

                        }
                        , error: function () {

                            //演示失败状态，并实现重传

                            var demoText = $('#demoText');

                            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');

                            demoText.find('.demo-reload').on('click', function () {

                                uploadInst.upload();
                            });
                        }
                    });
                    //提交按钮
                      form.on('submit(submitform)', function (data) {
                 
                  var EG_ID = $('#EG_ID').val();
                 
                  var index = parent.layer.getFrameIndex(window.name); //得到当前iframe层的索引
                 
                  var strdatas = $("#BIaodan").serializeArray();//序列化

                  var url = '/Present/PreInsert';
                  
                  if (parseInt(EG_ID) > 0) {
                      var url = '/Present/PreUpdate/' + EG_ID;
                }
                $.ajax({
                    type: "post",
                    url: url,
                    data:strdatas,
                    success: function (res) {
                        if (res.code == 0) {
                            parent.layer.msg(res.msg, { icon: 1 });
                            parent.layer.close(index); //再执行关闭

                        } else {
                            parent.layer.msg(res.msg, { icon: 5 });
                            parent.layer.close(index); //再执行关闭
                        }
                    },
                    error: function (res) {
                            parent.layer.msg(res.msg, { icon: 2 });
                        parent.layer.close(index); //再执行关闭
                    }
                })
                return false;
            });
                 });

            </script>

</body>
</html>
